package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.ProductCategoryExcution;
import com.fortune.dto.in.productCategory.PProduceCategoryAddDto;
import com.fortune.dto.in.productCategory.PProduceCategoryEditDto;
import com.fortune.enums.ProductCategoryEnum;
import com.fortune.service.ProductCategoryService;
import com.fortune.vo.productCategoryMgt.ProductCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "productCategoryMgt", description = "商品分类", tags = {"平台商品分类管理"})
@RestController
@RequestMapping("productCategoryMgt")
public class ProductCategoryMgtController {
	@Autowired
	private ProductCategoryService productCategoryService;
	@ApiOperation(value = "获取所有分类（树形结构）", nickname = "productCategoryMgtList", notes = "", tags={ "@曹然" })
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ProductCategoryVo>> list() {
		List<ProductCategoryVo> categories = productCategoryService.getTreeCategories();
		return new ProductCategoryExcution<List<ProductCategoryVo>>(categories,ProductCategoryEnum.SUCCESS);
	}

	@ApiOperation(value = "新增商品分类",nickname ="add",notes = "",tags = {"@石匡"})
	@PostMapping(value = "add",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<String> add(@Validated @RequestBody PProduceCategoryAddDto pProduceCategoryAddDto){
		if (pProduceCategoryAddDto.getPid()==null||pProduceCategoryAddDto.getPid().trim().equals("")) {
			pProduceCategoryAddDto.setPid(Constants.PRODUCT_ROOT_CATEGORY_PID);
		}
		int num = productCategoryService.queryProductCategoryByName(pProduceCategoryAddDto.getName());
		if (num > 0) {
			return new ProductCategoryExcution<String>(null, ProductCategoryEnum.NAME_EXIST);
		}
		String id = productCategoryService.insertProductCategory(pProduceCategoryAddDto);
		if (id==null) {
			return new ProductCategoryExcution<String>(null, ProductCategoryEnum.FAILURE);
		}
		return new ProductCategoryExcution<String>(id,ProductCategoryEnum.SUCCESS);
	}

	@ApiOperation(value = "编辑商品分类",nickname ="edit",notes = "",tags = {"@石匡"})
	@PutMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(@Validated @RequestBody PProduceCategoryEditDto pProduceCategoryEditDto){
		int num = productCategoryService.getCategoryById(pProduceCategoryEditDto.getId());
		if (num < 1) {
			return new ProductCategoryExcution<Void>(null, ProductCategoryEnum.NOT_EXSIST);
		}
		num = productCategoryService.editCategory(pProduceCategoryEditDto);
		if (num < 1) {
			return new ProductCategoryExcution<Void>(null, ProductCategoryEnum.FAILURE);
		}
		return new ProductCategoryExcution<Void>(null,ProductCategoryEnum.SUCCESS);
	}

	@ApiOperation(value = "删除商品分类",nickname ="delete",notes = "",tags = {"@石匡"})
	@ApiImplicitParam(name="id", value="商品分类id",paramType="query",dataType="string", required=true)
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@Validated @NotBlank(message="商品分类id不能为空") String id){
		int num = productCategoryService.getCategoryById(id);
		if (num < 1) {
			return new ProductCategoryExcution<Void>(null, ProductCategoryEnum.NOT_EXSIST);
		}
		num = productCategoryService.findCategoryByPro(id);
		if(num > 0){
			return new ProductCategoryExcution<Void>(null, ProductCategoryEnum.NOT_EXSISTS);
		}
		num = productCategoryService.deleteCategory(id);
		if (num < 1) {
			return new ProductCategoryExcution<Void>(null, ProductCategoryEnum.FAILURE);
		}
		return new ProductCategoryExcution<Void>(null,ProductCategoryEnum.SUCCESS);
	}
}
